package cn.cj.erp.common.vo.monitor;

import cn.cj.erp.common.security.security.user.LoginUser;
import cn.cj.erp.common.utils.MaskUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.util.Date;

/**
 * 当前在线会话
 *
 * @author chenjian
 */
@Data
public class SysUserOnlineVo {
    /** 用户id */
    private Long userId;

    /** token */
    private String token;

    /** 部门名称 */
    private String deptName;

    /** 用户名称 */
    private String username;

    /** 登录IP地址 */
    private String ip;

    /** 浏览器类型 */
    private String browser;

    /** 操作系统 */
    private String os;

    /** 登录时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date loginTime;

    public void hide() {
        this.ip = MaskUtil.maskIp(this.ip);
    }




    /**
     * 创建登录实体类
     * @param user 登录用户
     * @return SysUserOnlineDto
     */
    public static SysUserOnlineVo newInstance(LoginUser user) {
        if (ObjectUtil.isNull(user)) {
            return null;
        }
        SysUserOnlineVo sysUserOnline = new SysUserOnlineVo();
        sysUserOnline.setUserId(user.getUserId());
        sysUserOnline.setToken(user.getToken());
        sysUserOnline.setUsername(user.getUsername());
        sysUserOnline.setIp(user.getIp());
        sysUserOnline.setBrowser(user.getBrowser());
        sysUserOnline.setOs(user.getOs());
        sysUserOnline.setLoginTime(user.getLoginTime());
        if (StrUtil.isNotBlank(user.getUser().getDeptName())) {
            sysUserOnline.setDeptName(user.getUser().getDeptName());
        }
        return sysUserOnline;
    }
}
